package com.ftdigital.primemovers_operators.fragment;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.adpter.OilReportListViewAdapter;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.BaseModel;
import com.ftdigital.primemovers_operators.model.OilReprotItme;
import com.ftdigital.primemovers_operators.model.UserInfo;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.google.gson.JsonSyntaxException;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.json.JSONObject;

/**
 * 油量报表
 */
@EFragment(R.layout.fragment_oil_report)
public class OilReportFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.listview)
    RecyclerView listView;

    @ViewById(R.id.last_month_total)
    TextView lastMonthTotal;

    OilReportListViewAdapter oilReportListViewAdapter;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_oil_reprot));

        listView.setLayoutManager(new LinearLayoutManager(getActivity()));

        loadData();
    }

    private void loadData() {
        requestParams = "gasstation_id="+ MyApplication.userInfo.getData().getGasstation_id();
        requestUrl = RequestAddr.oilReprotList + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }
        });
    }

    OilReprotItme oilReprotItme;
    private void dealData(Object object) {
        try {
            oilReprotItme = gson.fromJson(object.toString(), OilReprotItme.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }

        oilReportListViewAdapter = new OilReportListViewAdapter(getActivity().getApplicationContext(), oilReprotItme);
        listView.setAdapter(oilReportListViewAdapter);

        listView.setLayoutParams(new LinearLayout.LayoutParams(listView.getLayoutParams().width, listView.getLayoutParams().height * oilReportListViewAdapter.getItemCount()));
        if (oilReprotItme.getData().getLast_months().equals("")){
            lastMonthTotal.setText("0");
        }else{
            lastMonthTotal.setText(oilReprotItme.getData().getLast_months());
        }

    }
    @Click(R.id.back_btn)
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
        }
    }

    protected String getCachePrifix(){
        return "OilReportFragment";
    }

}
